<?php

class Model_VenuesMapper
{
    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Model_DbTable_Venues');
        }
        return $this->_dbTable;
    }
    
    //find venue with venue id.
    public function find($venueId, Model_Venues $venue){
    	
    	$result = $this->getDbTable()->find($venueId);
        
		if (0 == count($result)) {
            return;
        }
        $row = $result->current();
		$venue->setVenue_id($row->venue_id);
		$venue->setVenue_name($row->venue_name);
		$venue->setVenue_image_url($row->venue_image_url);
		$venue->setVenue_des($row->venue_des);
		$venue->setParent($row->parent);
		$venue->setAdmin($row->admin);
    }
	//find the father id venue with venue id
    public function findParentId($venueId){
    	
    	$result = $this->getDbTable()->find($venueId);
    	if (0 == count($result)) {
            return -1;
        }
        $parentId = $result->current()->parent_id;
        return $parentId;
    }
    //find the children venue with venue id
    public function findChildrenInfo($venueId){
    	
    	$children = array();
    	
    	$selector = $this->getDbTable()->select();
    	$selector->from('venues')->where('parent=?', $venueId)->where('parent!=venue_id');
    	
    	$children = $this->findAllBySelector($selector);
    	
    	return $children;
    }
    //find all universities (the root venue)
    public function findUnversities(){
    	
    	$venues = array();
    	
    	$selector = $this->getDbTable()->select();
    	$selector->from('venues')->where('venue_id = parent');
    	
    	$venues = $this->findAllBySelector($selector);
    	return $venues;
    }
    //find the data with selector. Protected
    protected function findAllBySelector($selector){
    	
    	$results = $this->getDbTable()->fetchAll($selector);
    	
    	if(0==count($results)){
    		return;
    	}
    	$venues = array();
    	
    	foreach ($results as $row){
    		$row = $results->current();
    		$venue = new Model_Venues();
    		$venue->setVenue_id($row->venue_id);
    		$venue->setVenue_des($row->venue_des);
    		$venue->setVenue_name($row->venue_name);
    		$venue->setVenue_image_url($row->venue_image_url);
    		$venue->setAdmin($row->admin);
    		$venue->setParent($row->parent);
    		$venues[] = $venue;
    	}
    	return $venues;
    }
    //find all venues
    public function fetchAll(){
    	
    	$resultSet = $this->getDbTable()->fetchAll();
        $venues   = array();
        foreach ($resultSet as $row) {
            $venue = new Model_Venues();
    		$venue->setVenue_id($row->venue_id);
    		$venue->setVenue_des($row->venue_des);
    		$venue->setVenue_name($row->venue_name);
    		$venue->setVenue_image_url($row->venue_image_url);
    		$venue->setAdmin($row->admin);
    		$venue->setParent($row->parent);
    		$venues[] = $venue;
        }
        return $venues;
    }
    //find venue with the input letter of search bar
    public function findVenuesByPartial($phrase,$return_num)
    {
        $select = $this->getDbTable()->select();
        $select->from('venues')
                ->where('venue_name LIKE ?',"%$phrase%")
                ->order(array('venue_name ASC'));
        $entries = array();
        
		$entries = $this->findAllBySelector($select);
		return $entries;
    }
    //save venue info
    public function saveVenue(Model_Venues $venue){
    	
        $data = array(
        	'admin' => $venue->getAdmin(),
        	'venue_name' => $venue->getVenue_name(),
        	'venue_image_url' => $venue->getVenue_image_url(),
    		'venue_des' => $venue->getVenue_des(),
        	'parent' => $venue->getParent(),
        );

        if (null === ($id = $venue->getVenue_id())) {
        	// if no id input, then generate it and insert the data
            $this->getDbTable()->insert($data);
        } else {
        	// if an id input, then update the data which match this id
            $this->getDbTable()->update($data, array('venue_id = ?' => $id));
        }
    }
}

